package com.burt.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

/**
 * Description:奔驰G65
 * User: Burt
 * Date: 2017-05-24
 * Time: 9:35
 */
public class BenzG65 extends Car implements Serializable,Cloneable{

    /**
     * 座椅
     */
    private Seats seats;

    public BenzG65() {
        this.seats = new Seats();
        this.price = 1330000.00D;
        this.birthday = new Date();
    }

    /**
     * 克隆
     * @return
     */
    protected Object clone() throws CloneNotSupportedException {
        //深度克隆
        ByteArrayOutputStream bos = null;
        ObjectOutputStream oos = null;
        ByteArrayInputStream bis = null;
        ObjectInputStream ois = null;
        try {
            //return super.clone();//默认浅克隆，只克隆八大基本数据类型和String
            //序列化
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);
            oos.writeObject(this);

            //反序列化
            bis = new ByteArrayInputStream(bos.toByteArray());
            ois = new ObjectInputStream(bis);
            BenzG65 copy = (BenzG65)ois.readObject();
            copy.birthday = new Date(117,6,30);

            return copy;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally{
            try {
                bos.close();
                oos.close();
                bis.close();
                ois.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 生产G65
     */
    public void produce() {
        BenzG65 benzG65 = null;
        try {
            benzG65 = (BenzG65) clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        System.out.println("该车的出厂日期是：" + this.getBirthday());
        System.out.println("克隆车的出厂日期是：" + benzG65.getBirthday());
        System.out.println("该车与克隆车是否为同一对象：" + (this == benzG65));
        System.out.println("该车与克隆车是座椅是否为同一对象：" + (this.getSeats() == benzG65.getSeats()));//浅克隆为true，深克隆为false
    }

    public Seats getSeats() {
        return seats;
    }

    public void setSeats(Seats seats) {
        this.seats = seats;
    }
}
